package net.yeah.mickey_zhouyou.module.booklist.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.yeah.mickey_zhouyou.framework.component.GBC;
import net.yeah.mickey_zhouyou.framework.frame.MainFrame;
import net.yeah.mickey_zhouyou.module.booklist.domain.Book;

public class BookDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5009359129754854819L;

	private BookDialog() {
		super(MainFrame.getMainFrame(), "编辑", true);
		super.setSize(dialog_width, dialog_height);
		super.setResizable(false);
		resetLocation();
		super.setLayout(new BorderLayout());

		super.add(makeButtonPanel(), BorderLayout.SOUTH);
		super.add(makeGridBag(), BorderLayout.CENTER);
	}

	private JPanel makeGridBag() {
		JPanel res = new JPanel();
		res.setLayout(new GridBagLayout());

		res.add(new JLabel("标题"), new GBC(0, 0).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JLabel("副标题"), new GBC(0, 1).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JLabel("版本"), new GBC(0, 2).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JTextField(), new GBC(1, 0).setWeight(100, 0).setFill(GBC.BOTH));
		res.add(new JTextField(), new GBC(1, 1).setFill(GBC.BOTH));
		res.add(new JTextField(), new GBC(1, 2).setFill(GBC.BOTH));
		
		res.add(new JLabel("出版社"), new GBC(2, 0).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JLabel("作者"), new GBC(2, 1).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JLabel("译者"), new GBC(2, 2).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JTextField(), new GBC(3, 0).setFill(GBC.BOTH));
		res.add(new JTextField(), new GBC(3, 1).setWeight(100, 0).setFill(GBC.BOTH));
		res.add(new JTextField(), new GBC(3, 2).setFill(GBC.BOTH));

		res.add(new JLabel("简介"), new GBC(0, 3, 2, 1).setWeight(100, 0).setAnchor(GBC.WEST));
		res.add(new JScrollPane(new JTextArea()), new GBC(0, 4, 2, 1).setWeight(100, 100).setFill(GBC.BOTH));
		
		res.add(new JLabel("备注"), new GBC(2, 3, 2, 1).setWeight(100, 0).setAnchor(GBC.WEST));
		res.add(new JScrollPane(new JTextArea()), new GBC(2, 4, 2, 1).setWeight(100, 100).setFill(GBC.BOTH));

		res.add(new JLabel("目录"), new GBC(4, 1).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JScrollPane(new JTextArea()), new GBC(4, 2, 3, 4).setWeight(100, 100).setFill(GBC.BOTH));

		res.add(new JLabel("选择分类"), new GBC(4, 0).setWeight(10, 0).setAnchor(GBC.WEST));
		res.add(new JScrollPane(new JTextArea()), new GBC(5, 0).setWeight(100, 0).setFill(GBC.BOTH));
		res.add(new JButton("..."), new GBC(6, 0).setWeight(5, 0).setFill(GBC.BOTH));
		return res;
	}

	private JPanel makeButtonPanel() {
		JPanel res = new JPanel();
		res.add(new JButton("保存"));
		return res;
	}

	private final int dialog_width = 950;
	private final int dialog_height = 600;

	private Book book = null;

	private static BookDialog dialog = null;

	public static BookDialog getDialog() {
		if (dialog == null) {
			synchronized (BookDialog.class) {
				if (dialog == null)
					dialog = new BookDialog();
			}
		}
		return dialog;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	private void resetLocation() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		final int x = (screenSize.width - dialog_width) / 2;
		final int y = (screenSize.height - dialog_height) / 2;

		super.setLocation(x, y);
	}

	@Override
	public void setVisible(boolean b) {
		super.setVisible(b);
		if (b)
			resetLocation();
	}

}
